﻿<%@ WebHandler Language="C#" Class="ReportTongJiTotalTypeHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Collections;

public class ReportTongJiTotalTypeHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        int typeId = 0;
        typeId = int.Parse(context.Request.QueryString["typeId"]);
        KVModel model;


        string str = System.Configuration.ConfigurationManager.ConnectionStrings["decheatConnectionString"].ToString();
        string sql = "exec ReportTongJiTotalType " + typeId;
        Hashtable hash = new Hashtable();
        SqlConnection conn = new SqlConnection(str);

        
        conn.Open();
        SqlCommand comm = new SqlCommand(sql, conn);
        System.Data.DataSet ds = new DataSet();
        SqlDataReader dr = comm.ExecuteReader();
        while (dr.Read())
        {

            string typename = dr["typename"].ToString();
            int num = int.Parse(dr["couttimes"].ToString());
            string provincename = dr["provincename"].ToString();
            if (!hash.ContainsKey(provincename))
            {
                hash[provincename] = num;
            }
            else
            {
                hash[provincename] = int.Parse(hash[provincename].ToString()) + num;
            }
        }

        dr.Close();
        List<KVModel> resultList = new List<KVModel>();
        foreach (string key in hash.Keys)
        {
            int num = int.Parse(hash[key].ToString());
            model = new KVModel((key.Split(new string[] { "省" }, StringSplitOptions.None))[0], num);
            resultList.Add(model);
        }
        
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
        }
        //JavaScriptSerializer json = new JavaScriptSerializer();
        string result = WebUI.CommonClass.JSONHelper.Obj2Json<List<KVModel>>(resultList);
        context.Response.ContentType = "text/plain";
        context.Response.Write(result);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}